<template>
  <div>
    <el-form :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="省">
        <el-select @change="fetchCities" v-model="searchForm.province" placeholder="请选择省">
          <el-option
            v-for="item in provinces"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="市">
        <el-select @change="fetchAreas" v-model="searchForm.city" placeholder="请选择市">
          <el-option
            v-for="item in cities"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="区域">
        <el-select v-model="searchForm.area" placeholder="请选择区域">
          <el-option
            v-for="item in areas"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="searchForm.status" placeholder="请选择状态">
          <el-option label="已审核" value="已审核"></el-option>
          <el-option label="未审核" value="未审核"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="searchForm.realName" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" @click="batchAgree">批量同意</el-button>
    <el-button type="danger" @click="batchRefuse">批量拒绝</el-button>
    <el-table :data="tableData" @selection-change="handleSelectionChange" style="width: 100%">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="realName" label="姓名"></el-table-column>
      <el-table-column prop="requestedArea" label="区域"></el-table-column>
      <el-table-column prop="status" label="状态"></el-table-column>
      <el-table-column prop="annualIncome" label="家庭收入"></el-table-column>
      <el-table-column prop="housingArea" label="住房面积"></el-table-column>
      <el-table-column label="操作" width="300">
        <template slot-scope="scope">
          <el-button-group>
            <el-button size="mini" @click="viewDetails(scope.row)">查看详情</el-button>
            <el-button
              size="mini"
              v-if="scope.row.status === '未审核'"
              type="success"
              @click="approve(scope.row)"
            >同意</el-button>
            <el-button
              size="mini"
              v-if="scope.row.status === '未审核'"
              type="danger"
              @click="reject(scope.row)"
            >拒绝</el-button>
            <el-button size="mini" v-if="scope.row.status === '已审核'" type="danger">撤销同意</el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-if="tableData.length > 0"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <div v-else style="text-align: center; margin-top: 20px;">
      <el-empty description="暂无待审核租客申请"></el-empty>
    </div>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <p v-if="selectedRows.length > 0">确定拒绝以下租客[{{ selectedAccount }}]的申请吗？</p>
      <p v-else>确定拒绝该申请吗？</p>
      <span>请输入拒绝理由：</span>
      <el-input v-model="rejectReason" placeholder="请输入拒绝理由"></el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleRejectConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
  
  <script>
export default {
  data() {
    return {
      searchForm: {
        province: "",
        city: "",
        requestedArea: "",
        status: "",
        realName: ""
      },
      currentPage: 1,
      pageSize: 5,
      total: "",
      tableData: [],
      provinces: [],
      cities: [],
      areas: [],
      dialogVisible: false,
      selectRow: {},
      rejectReason: "",
      selectedRows: [],
      selectedAccount: ""
    };
  },
  mounted() {
    this.fetchProvinces();
    this.search();
  },
  methods: {
    fetchProvinces() {
      this.$axios.post("area/province", { none: "none" }).then(res => {
        if (res.data.code === 200) {
          this.provinces = [];
          res.data.data.forEach(province => {
            this.provinces.push({ value: province.name, label: province.name });
          });
        } else {
          this.$message.error(res.data.message);
        }
      });
    },
    fetchCities() {
      this.searchForm.city = "";
      this.$axios
        .post("area/city", { province: this.searchForm.province })
        .then(res => {
          if (res.data.code === 200) {
            this.cities = [];
            res.data.data.forEach(city => {
              this.cities.push({ value: city.name, label: city.name });
            });
          } else {
            this.$message.error(res.data.message);
          }
        });
    },
    fetchAreas() {
      this.searchForm.area = "";
      this.$axios
        .post("area/query", {
          province: this.searchForm.province,
          city: this.searchForm.city,
          name: ""
        })
        .then(res => {
          if (res.data.code === 200) {
            this.areas = [];
            res.data.data.forEach(area => {
              this.areas.push({ value: area.name, label: area.name });
            });
          } else {
            this.$message.error(res.data.message);
          }
        });
    },
    handleSizeChange(size) {
      this.pageSize = size;
      this.fetchData();
    },
    handleCurrentChange(page) {
      this.currentPage = page;
      this.fetchData();
    },
    search() {
      let curr =
        this.searchForm.name == "" ? (this.currentPage - 1) * this.pageSize : 0;
      this.$axios
        .post("audit/get", {
          ...this.searchForm,
          size: this.pageSize,
          curr
        })
        .then(res => {
          if (res.data.code === 200) {
            this.tableData = res.data.data;
            this.total = res.data.total;
          } else {
            this.$message.error(res.data.message);
          }
        })
        .catch(err => {
          this.$message.error(err.message);
        });
    },
    viewDetails(row) {
      this.$router.push("/auditDetail");
      this.$store.commit("setAuditDetail", row);
    },
    handleSelectionChange(val) {
      this.selectedRows = val;
    },
    batchAgree() {
      if (this.selectedRows.length === 0) {
        this.$message.warning("请选择要审核的用户");
        return;
      }
      this.selectedRows.forEach(row => {
        row.status = "已审核";
      });
      this.sendAuditRequest(this.selectedRows);
    },
    batchRefuse() {
      if (this.selectedRows.length === 0) {
        this.$message.warning("请选择要审核的用户");
        return;
      }
      this.selectedRows.forEach(row => {
        row.status = "保存";
      });
      this.dialogVisible = true;
    },
    approve(row) {
      this.$confirm("确定同意该申请吗？", "提示", {
        type: "warning"
      })
        .then(() => {
          row.status = "已审核";
          this.sendAuditRequest([row]);
        })
        .catch(() => {});
    },
    reject(row) {
      this.dialogVisible = true;
      this.selectRow = row;
    },
    handleRejectConfirm() {
      this.dialogVisible = false;
      let confirm =
        this.selectedRows.length > 0 ? this.selectedRows : [this.selectRow];
      confirm.forEach(row => {
        row.status = "保存";
        row.reason = this.rejectReason;
      });
      if (this.rejectReason) {
        this.sendAuditRequest(confirm);
      } else {
        this.$message.error("请输入拒绝理由");
      }
    },
    sendAuditRequest(data) {
      this.$axios
        .post("audit/audit", data)
        .then(res => {
          if (res.data.code === 200) {
            this.$message.success("操作成功");
            this.search();
          } else {
            this.$message.error(res.data.message);
          }
        })
        .catch(err => {
          this.$message.error(err.message);
        });
    }
  }
};
</script>
  
  <style scoped>
.demo-form-inline .el-form-item {
  margin-right: 10px;
}
</style>
  